const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_login: false,
    is_info:true,
    options: {},
    userInfo: {}, // 用户信息
    name:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    _this.setData({
      options
    });
    // 获取当前用户信息
    _this.getUserDetail();
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function(result) {
      if(result.data.userInfo.is_supplier == 1 && result.data.userInfo.supplier_id == 0){
        //填写注册信息
        _this.setData({
          is_login: true,
          is_info:false,
        })
      }else if(result.data.userInfo.is_supplier == 0){
        _this.setData({
          is_login: false,
          is_info: true,
        })
      }else{
        wx.redirectTo({
          url: '/pages/supplier/index',
        })
      }
    });
  },
  getPhoneNumber: function (e) {
    var _this = this;
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      console.log(e)
      let encryptedData = encodeURIComponent(e.detail.encryptedData)
      let iv = e.detail.iv
      App._post_form('user/phoneSupplier', {
        encryptedData: encryptedData,
        iv: iv,
      }, result => {
        console.log(result.data.userInfo)
        if(result.data.userInfo.is_supplier == 1 && result.data.userInfo.supplier_id == 0){
          //填写注册信息
          _this.setData({
            is_login: true,
            is_info:false,
          })
        }else{
          wx.showModal({
            title: '友情提示',
            content: '授权失败',
            showCancel: false,
          });
        }
      }, false, () => {
        wx.hideLoading();
      });
    }

  },
  /**
   * 暂不登录
   */
  onNotLogin() {
    let _this = this;
    // 跳转回原页面
    _this.onNavigateBack(_this.data.options.delta);
  },
  /**
   * 输入真实姓名
   */
  contentInput: function(e) {
    let _this = this
    let index = e.currentTarget.dataset.index;
    _this.setData({
      name:e.detail.value
    })
  },
  submit:function(){
    let _this = this
    if(_this.data.name == ''){
      App.showError('请输入微信实名认证真实姓名')
      return false;
    }
    App._post_form('user/regSupplier', {
      name: _this.data.name
    }, function(result) {
      if(result.data.userInfo.supplier_id > 0){
        wx.redirectTo({
          url: '/pages/supplier/index',
        })
      }
    },
    false,
    function() {
      wx.hideLoading();
    });
  }
})